home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / AMIGA.C < prev    next >
C/C++ Source or Header  |  1990-03-11  |  13KB  |  711 lines

  1. /*
  2.  * NOTE: Portions of this code was directly derived from the Amiga C-Kermit
  3.  * and is copyrighted by Jack J. Rouse.  Please read the following Copyright
  4.  * notice.
  5.  *                                Mikel Matthews
  6.  
  7.  Author: Jack Rouse
  8.  Contributed to Columbia University for inclusion in C-Kermit.
  9.  Copyright (C) 1986, Jack J. Rouse, 106 Rubin Ct. Apt. A-4, Cary NC 27511
  10.  Permission is granted to any individual or institution to use, copy, or
  11.  redistribute this software so long as it is not sold for profit, provided 
  12.  this copyright notice is retained. 
  13.  
  14.  The file status routines assume all file protection modes are real, instead
  15.  of just delete protection on files and write protection on disks.
  16. */
  17. #include <stdio.h>        /* standard I/O stuff */
  18. #include "exec/types.h"
  19. #include "exec/exec.h"
  20. #include "devices/serial.h"
  21. #include "devices/timer.h"
  22. #include "libraries/dos.h"
  23. #include "libraries/dosextens.h"
  24. #include "intuition/intuition.h"
  25. #include "intuition/intuitionbase.h"
  26. #include "amiga.h"
  27. #include "fcntl.h"
  28. #include "global.h"
  29. #include "iface.h"
  30. #include "smtp.h"
  31. #include "dir.h"
  32. extern    errno;
  33. struct Process *MyProc;
  34. struct CommandLineInterface *MyCLI;
  35. struct FileHandle *Fptr;    /* used for input */
  36.  
  37. /* static definitions */
  38.  
  39. static struct MsgPort *serport;        /* message port for serial comm */
  40. static struct IOExtSer *Read_Request;    /* serial input request */
  41. static struct IOExtSer *Write_Request;    /* serial output request */
  42. static serialopen;
  43.  
  44. /* Exec routines */
  45.  
  46. APTR AllocMem();
  47. struct IORequest *CheckIO();
  48. VOID CloseDevice();
  49. LONG DoIO();
  50. struct Task *FindTask();
  51. struct Message *GetMsg();
  52. LONG OpenDevice();
  53. struct Library *OpenLibrary();
  54. VOID ReplyMsg();
  55. VOID SendIO();
  56. LONG Wait();
  57. LONG WaitIO();
  58. struct Message *WaitPort();
  59.  
  60. /* Exec support */
  61. struct MsgPort *CreatePort();
  62. VOID DeletePort();
  63.  
  64. /* AmigaDOS routines */
  65. BPTR Input();
  66. LONG Read();
  67. LONG WaitForChar();
  68.  
  69. struct asy asy[ASY_MAX];
  70.  
  71. extern x, y, maxcol, MINX, MINY, MAXX;
  72. struct MsgPort *mySerPort;
  73. struct MsgPort *myTimerPort;
  74.  
  75. extern char *mailqdir;
  76.  
  77. struct Store_input {
  78.     char *head;
  79.     char store[MAX_STORE];
  80.     char *tail;
  81.     int    amt;
  82. } Store_input[ASY_MAX];
  83.  
  84. /* Interface list; initialized at compile time to contain
  85.  * just the local interface
  86.  */
  87. struct interface *ifaces;
  88.  
  89. char *ttbuf;
  90.  
  91. char Amiga_drive[255];
  92. char    con_buff[255];
  93. /*
  94.  *  sysinit -- Amiga specific initialization
  95.  */
  96. ioinit() 
  97. {
  98.     register struct IOExtSer *iob;
  99.     int    i;
  100.     int speed;
  101.     char *ptr;
  102.     FILE *ffptr;
  103.     int    fd;
  104.     extern char version[];
  105.  
  106.  
  107.     MyProc = (struct Process *)FindTask((char *)NULL);
  108.  
  109.     MyCLI = (struct CommandLineInterface *)BADDR(MyProc->pr_CLI);
  110.  
  111.     /* allocate serial ports and IO blocks */
  112.     (void) Execute("cd >RAM:tmpfilename", NULL, NULL);
  113.  
  114.     if ( ( ffptr = fopen("RAM:tmpfilename", "r") ) == NULL)
  115.     {
  116.         return;
  117.     }
  118.  
  119.     (void) fgets( Amiga_drive, sizeof(Amiga_drive), ffptr);
  120.     (void) fclose(ffptr);
  121.     (void) unlink("RAM:tmpfilename");
  122.     ptr = Amiga_drive;
  123.     while ( *ptr )
  124.     {
  125.         if (*ptr == '\n')
  126.         {
  127.             *ptr = '\0';
  128.         }
  129.         else if (*ptr == ':')
  130.         {
  131.             *++ptr = '\0';
  132.         }
  133.         else
  134.             ptr++;
  135.     }
  136.  
  137. }
  138.  
  139.  
  140. iostop(str, val)
  141. char *str;
  142. int val;
  143. {
  144.     /* close everything */
  145.     /*
  146.     printf("%s, error = %d\n\r", str, val);
  147.     */
  148.  
  149.     if (serialopen)
  150.     {
  151.         CloseDevice(Read_Request);
  152.     }
  153.     if ( mySerPort )
  154.     {
  155.         DeletePort(mySerPort);
  156.     }
  157.  
  158. }
  159. char Receive_store[MAX_STORE];
  160. char Output_store[MAX_STORE];
  161.  
  162. asy_init(dev, arg1,arg2,bufsize)
  163. int dev, bufsize;
  164. char *arg1,*arg2;
  165. {
  166.     register struct asy *ap;
  167.     register struct interface *if_asy;
  168.     extern struct interface *ifaces;
  169.     register struct IOExtSer *iob;
  170.     char *b, *c;
  171.     int i;
  172.     struct Store_input *store;
  173.     iob = Read_Request;
  174.     
  175.     /*
  176.     printf("ASY_INIT: dev = %d\n\r", dev);
  177.     */
  178.     
  179.     ap = &asy[dev];
  180.  
  181.     if ( ap == NULL)
  182.     {
  183.         printf("asy_init: returing -1\n");
  184.         return(-1);
  185.     }
  186.     ap->tty = malloc(strlen(arg2)+1);
  187.     strcpy(ap->tty, arg2);
  188.  
  189.     if (serialopen)
  190.     {
  191.         printf("asy_init: serial line already open\n");
  192.         return(0);    /* ignore if already open */
  193.     }
  194.  
  195.     Read_Request = (struct IOExtSer *)
  196.         AllocMem((long) sizeof(*Read_Request), MEMF_PUBLIC | MEMF_CLEAR);
  197.  
  198.  
  199.     Read_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  200.  
  201.     if (OpenDevice(SERIALNAME, NULL, Read_Request, NULL))
  202.     {
  203.         iostop("Cant open Read device", 5L);
  204.     }
  205.  
  206.     setmem(&Read_Request->io_TermArray, sizeof(struct IOTArray), 0xc0);
  207.     Read_Request->io_SerFlags = SERF_XDISABLED | SERF_EOFMODE;
  208.     Read_Request->io_Baud = 9600L;
  209.     Read_Request->io_ReadLen = 8L;
  210.     Read_Request->io_WriteLen = 8L;
  211.     Read_Request->io_CtlChar = 0x00;
  212.     Read_Request->io_RBufLen = sizeof(Receive_store);
  213.     Read_Request->io_BrkTime = 0L;
  214.     Read_Request->IOSer.io_Command = SDCMD_SETPARAMS;
  215.  
  216.     DoIO(Read_Request);
  217.  
  218.     Read_Request->IOSer.io_Command = CMD_READ;
  219.     Read_Request->IOSer.io_Length = 1L;
  220.     Read_Request->IOSer.io_Data = (APTR)Receive_store;
  221.  
  222.     Write_Request = (struct IOExtSer *)
  223.         AllocMem((long) sizeof(*Write_Request), MEMF_PUBLIC | MEMF_CLEAR);
  224.  
  225.     b = (BYTE *) Read_Request;
  226.     c = (BYTE *) Write_Request;
  227.  
  228.     for ( i = 0; i < sizeof (struct IOExtSer); i++)
  229.     {
  230.         *c++ = *b++;
  231.     }
  232.  
  233.     Write_Request->io_SerFlags = SERF_XDISABLED;
  234.     Write_Request->IOSer.io_Message.mn_ReplyPort = CreatePort(0L, 0L);
  235.     Write_Request->IOSer.io_Command = CMD_WRITE;
  236.     Write_Request->IOSer.io_Length = 1L;
  237.     Write_Request->IOSer.io_Data = (APTR) Output_store;
  238.  
  239.     store = &Store_input[dev];
  240.     store -> head = store -> tail = store -> store;
  241.     store -> amt = 0;
  242.  
  243.     serialopen = TRUE;
  244.  
  245.     return (0L);
  246.  
  247. }
  248.  
  249. asy_stop(interface)     /* ttclos() */
  250. struct interface *interface;
  251. {
  252.     register struct asy *ap;
  253.  
  254.     if (!serialopen)
  255.     {
  256.         return(0);
  257.     }
  258.     CloseDevice(Read_Request);
  259.     serialopen = FALSE;
  260.     return(0);
  261. }
  262.  
  263.  
  264. asy_ioctl(interface, argc, argv)
  265. struct interface *interface;
  266. int argc;
  267. char *argv[];
  268. {
  269.     if ( argc < 1 ) 
  270.     {
  271.         printf("%d\n", asy[interface->dev].speed);
  272.         return 0;
  273.     }
  274.  
  275.     return ( asy_speed( interface->dev, atoi(argv[0])));
  276. }
  277.  
  278. asy_speed(dev, baud)
  279. int dev, baud;
  280. {
  281.     register struct IOExtSer *iob = Read_Request;
  282.     int    speed;
  283.     int e;
  284.  
  285.     /*
  286.     printf("asy_speed, baud = %d\n\r", baud);
  287.     */
  288.     
  289.     if(baud == 0 || dev >= nasy)
  290.     {
  291.         printf("Baud = %d, dev = %d, returning BAD!!\n", baud, dev);
  292.         return(-1);
  293.     }
  294.     asy[dev].speed = baud;
  295.     /*
  296.     printf("serialopen = %d\n", serialopen);
  297.     */
  298.     if (!serialopen ) 
  299.         return(-1);
  300.  
  301.     /* fill in parameters */
  302.  
  303.     iob->io_CtlChar = 0x1113000L;
  304.  
  305.     if (baud >= 0L && baud <= 19200L) 
  306.     {
  307.         iob->io_Baud = baud;
  308.     }
  309.  
  310.     setmem(&iob->io_TermArray, sizeof(struct IOTArray), 0x12);
  311.  
  312.     iob->io_ReadLen = iob->io_WriteLen = 8L;
  313.     iob->io_StopBits = 1L;
  314.     iob->io_SerFlags = SERF_XDISABLED ;
  315.  
  316.     if ( baud > 19200L)
  317.     {
  318.         iob->io_SerFlags |= SERF_RAD_BOOGIE;
  319.     }
  320.     else
  321.     {
  322.         iob->io_SerFlags &= ~SERF_RAD_BOOGIE;
  323.     }
  324.  
  325.     iob->io_SerFlags &= ~(SERF_PARTY_ON | SERF_EOFMODE);
  326.  
  327.     /* set the parameters */
  328.     
  329.     iob->IOSer.io_Command = SDCMD_SETPARAMS;
  330.  
  331.     DoIO(iob);
  332.     return(0);
  333. }
  334.  
  335.  
  336. asy_output(dev, buf, n)
  337. int dev;
  338. char *buf;
  339. int n;
  340. {
  341.     int err;
  342.     register struct IOExtSer *write = Write_Request;
  343. /*    
  344.     printf("asy_output\n");
  345.     printf("asy_output: dev = %d, count = %d\n", dev, n);
  346.     printf("asy_output: serialopen = %d\n", serialopen);
  347. */    
  348.  
  349.     if (!serialopen) 
  350.     {
  351.         printf("asy_output: returning -1\n");
  352.         return(-1);
  353.     }
  354.     write->IOSer.io_Command = CMD_WRITE;
  355.     write->IOSer.io_Data    = (APTR)buf;
  356.     write->IOSer.io_Length  = (long)n;
  357.     if ( ( err = DoIO(write)) != 0)
  358.     {
  359.         printf("asy_output: err = %d\n", err);
  360.     }
  361.     return(n);
  362. }
  363.  
  364. asy_recv(dev,buf,cnt)
  365. int dev;
  366. char *buf;
  367. unsigned cnt;
  368. {
  369.     register struct IOExtSer *read = Read_Request;
  370.     int tot = 0;
  371.     struct Store_input *store;
  372.     char store_it[MAX_STORE];
  373.     int got = 0;
  374.     int tt, e;
  375.     struct asy *ap;
  376.  
  377. #ifdef DEBUG
  378. printf("asy_recv: dev = %d\n", dev);    
  379. #endif
  380.  
  381.     if ( dev > nasy )
  382.     {
  383.         printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
  384.         return(-1);
  385.     }
  386.  
  387.     ap = &asy[dev];
  388.  
  389.     store = &Store_input[dev];        /* point to current */
  390.  
  391. /* printf("asy_recv: want %d, buffer has %d bytes\n", cnt, store->amt);    */
  392.  
  393.     if (